﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

//Tal
namespace InformationExtraction
{
    class CountryProcess : ExtractorProcess
    {
        static List<string> nationalities;

        static CountryProcess()
        {
            nationalities = new List<string>();
            StreamReader sr = new StreamReader("nationalities.txt");
            while (!sr.EndOfStream)
                nationalities.Add(sr.ReadLine());
            sr.Close();
        }

        public override void Execute(Person person, Page page)
        {
            //((he)|(she)|(" + Regex.Escape(page.Title) + @"))?
            Regex regex = new Regex(@"(is|was)/\S+ (a|an)/\S+[^.]*? (\w+)/JJ");
            MatchCollection mc = regex.Matches(page.RawContent);
            for (int i = 0; i < mc.Count; i++)
            {
                if (nationalities.Contains(mc[i].Groups[3].Value))
                {
                    person.Country = mc[i].Groups[3].Value;
                    break;
                }
            }

            if (person.Country == null)
            {
                regex = new Regex(@"(\w+)/JJ");
                mc = regex.Matches(page.RawContent);
                for(int i=0;i<mc.Count;i++)
                {
                    if(nationalities.Contains(mc[i].Groups[1].Value))
                    {
                        person.Country = mc[i].Groups[1].Value;
                        break;
                    }
                }
            }

            
        }
    }
}
